﻿/*---------------------------------------------------------------------*/
// fileName="DirectoryService.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

using SureKAM.BackUp.Notify.Domain;

namespace SureKAM.BackUp.Notify.Service
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SureKAM.BackUp.Notify.Service.Interfaces;
    using SureKAM.BackUp.Notify.DataAccess;
    using Microsoft.Practices.Unity;

    /// <summary>
    /// Directory服务类
    /// </summary>
    public class DirectoryService : IDirectoryService
    {
        #region 私有字段
        private IDirectory directory;
        private IDNMapping dnMapping;
        #endregion

        #region 构造方法
        public DirectoryService()
        {
            this.directory = DataAccessIOC.Current.Resolve<IDirectory>();
            this.dnMapping = DataAccessIOC.Current.Resolve<IDNMapping>();
        }
        #endregion

        #region 公有方法
        /// <summary>
        /// 添加一个directory对象
        /// </summary>
        /// <param name="directory">directory对象</param>
        /// <returns>返回添加directory对象的ID</returns>
        public string Add(Domain.Directory directory)
        {
            this.directory.InsertDirectory(directory);
            this.directory.SaveToFile();
            return directory.ID;
        }

        /// <summary>
        /// 更新一个directory对象
        /// </summary>
        /// <param name="directory">directory对象</param>
        /// <returns>返回更新的对象</returns>
        public Domain.Directory Update(Domain.Directory directory)
        { 
            Domain.Directory operation= this.directory.UpdateDirectory(directory);
            this.directory.SaveToFile();
            return operation;
        }

        /// <summary>
        /// 删除一个direct对象
        /// </summary>
        /// <param name="directory">driectory对象</param>
        /// <returns>返回删除对象的ID</returns>
        public string Delete(Domain.Directory directory)
        {
            string operation = string.Empty;
            if (directory != null)
            {
                List<Notify.Domain.DirectoryNotifyMapping> directoryNotifyMappings = this.dnMapping.GetDNs();
                if (directoryNotifyMappings != null)
                {
                    for (int i = 0; i < directoryNotifyMappings.Count; i++)
                    {
                        if (directoryNotifyMappings[i].DirectoryId == directory.ID)
                        {
                            this.dnMapping.DeleteDN(directoryNotifyMappings[i]);
                        }
                    }
                }
            }
            this.dnMapping.SaveToFile();
            operation = this.directory.DeleteDirectoryById(directory.ID);
            this.directory.SaveToFile();
            return operation;
        }

        /// <summary>
        /// 根据ID返回相应的Directory对象
        /// </summary>
        /// <param name="id">Directory Id</param>
        /// <returns></returns>
        public Domain.Directory GetDirectoryById(string id)
        {
            Domain.Directory directory=this.directory.GetDirectiryById(id);
            if(directory!=null)
            {
                directory.Notifys = this.directory.GetNotifys(directory.ID);
            }
            return directory;
        }

        /// <summary>
        /// 返回所有的Directory对象
        /// </summary>
        /// <returns></returns>
        public List<Domain.Directory> GetDirectories()
        {
            List<Domain.Directory> directories = this.directory.GetDirectorys();
            if(directories!=null&&directories.Count>0)
            {
                foreach (Directory directory1 in directories)
                {
                    directory1.Notifys = this.directory.GetNotifys(directory1.ID);
                }
            }
            return directories;
        }

        /// <summary>
        /// 添加目录与通知者的映射
        /// </summary>
        /// <param name="directoryId">Directory Id</param>
        /// <param name="notifyId">Notify Id</param>
        /// <returns>返回True 成功 False 失败</returns>
        public bool AddDNMapping(string directoryId, string notifyId)
        {
            bool operation = false;
            List<Domain.DirectoryNotifyMapping> directoryNotifyMappings = this.dnMapping.GetDNs();
            var result = from mapping in directoryNotifyMappings
                         where mapping.NotifyId == notifyId && mapping.DirectoryId == directoryId
                         select mapping;
            if(result.Count()==0)
            {
                DirectoryNotifyMapping directoryNotifyMapping=new DirectoryNotifyMapping();
                directoryNotifyMapping.Id = Guid.NewGuid().ToString();
                directoryNotifyMapping.DirectoryId = directoryId;
                directoryNotifyMapping.NotifyId = notifyId;
                this.dnMapping.InsertDN(directoryNotifyMapping);
                this.dnMapping.SaveToFile();
                operation = true;
            }
            return operation;
        }

        /// <summary>
        /// 删除目录与通知者的映射
        /// </summary>
        /// <param name="driectoryId">directory id</param>
        /// <param name="notifyId">notify id</param>
        /// <returns>返回true 成功 False失败</returns>
        public bool DeleteDNMapping(string directoryId, string notifyId)
        {
            bool operation = false;
            List<Domain.DirectoryNotifyMapping> directoryNotifyMappings = this.dnMapping.GetDNs();
            var result = from mapping in directoryNotifyMappings
                         where mapping.NotifyId == notifyId && mapping.DirectoryId == directoryId
                         select mapping;
            if(result.Count()>0)
            {
                DirectoryNotifyMapping directoryNotifyMapping = result.FirstOrDefault();
                this.dnMapping.DeleteDN(directoryNotifyMapping);
                this.dnMapping.SaveToFile();
                operation = true;
            }
            return operation;
        }
        #endregion



    }
}
